/**
 *
 */
package de.cbf.cam.dsa.facet.container;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import de.cbf.util.entity.facet.constants.ConstantsFontColorLogView;
import de.cbf.util.entity.facet.entities.FacetOfEntity;
import de.cbf.util.entity.facet.util.TextPartStyled;
import de.cbf.util.uuidheader.entities.UUIDHeader;
import de.cbf.util.uuidheader.exceptions.EntityMergeException;

/**
 * @author Nebelritter 
 * LATER needs x,y,z for max dimensions of held item
 * LATER, when used make own ContainerState Facet for items inside and container open/closed attributes
 */
@XmlRootElement(name="container")
@XmlAccessorType(XmlAccessType.FIELD)
public class FacetContainer extends FacetOfEntity implements FacetContainerConstants{
	protected String		fVolume ;	
	@XmlElementWrapper(name="for_types")
	@XmlElement(name="type")
	protected List<String>	fForTypes; //LATER some how use type constants/enums to identify types


	public FacetContainer() {
		super();
	}

	public FacetContainer(String name) {
		super(name);
	}

	@Override
	protected void init() {
		super.init();
		fVolume 	 = UNSET_VOLUME;
		fForTypes = new ArrayList<String>();
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.entity.facet.entities.FacetOfEntity#doMergeDataFrom(de.cbf.util.uuidheader.entities.UUIDHeader, boolean)
	 */
	@Override
	protected void doMergeDataFrom(UUIDHeader entity, boolean i_am_older)
			throws EntityMergeException {
		super.doMergeDataFrom(entity, i_am_older);
		if (entity instanceof FacetContainer) {
			FacetContainer other = (FacetContainer) entity;
			if(i_am_older){
				this.fVolume = other.fVolume;
				this.fForTypes.clear();
				this.fForTypes.addAll(other.fForTypes);				
			}
		}else{
			String mergeEntityClass = entity == null? "null":entity.getClass().getName();
			String errorMsg ="EntityToMerge "+mergeEntityClass+" does not fit class:"+this.getClass().getName();
			throw new EntityMergeException(errorMsg);
		}
	}
	
	@Override
	public List<TextPartStyled> getFacetStyledDescription() {
		
		List<TextPartStyled> textStyleInfo = new ArrayList<TextPartStyled>();
		
		TextPartStyled textPart;
			
		//container heading
		textPart = new TextPartStyled();
		textPart.setText(TEXT_LOG_HEADING_CONTAINER+"\n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_FACET);		
		textStyleInfo.add(textPart );
		
		//volume
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_VOLUME);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart );
		//volume value
		textPart = new TextPartStyled();
		textPart.setText(fVolume+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart );

		return textStyleInfo;
	}
	
	@Override
	public String hasUnsetValues() {
		StringBuffer result = new StringBuffer();
		if(UNSET_VOLUME.equals(fVolume)){
			result.append(TEXT_UNSET_Volume+"\n");
		}
		if(UNSET_ForTypes.equals(fForTypes)){
			result.append(TEXT_UNSET_ForTypes+"\n");
		}
		return result.toString();
	}

	public String getVolume() {
		return fVolume;
	}

	public void setVolume(String newVolume) {
		if(fVolume != null && fVolume.equals(newVolume)){
            return;
        }
        String oldVolume = fVolume;
        fVolume = newVolume;
        propertyChangeSupport.fireIndexedPropertyChange(PN_VOLUME, PI_VOLUME, oldVolume, fVolume);
	}

	public List<String> getForTypes(){
		return fForTypes;
	}

	public boolean addForType(String item){
		boolean result = fForTypes.add(item);
		propertyChangeSupport.fireIndexedPropertyChange(PN_FOR_TYPES, PI_FOR_TYPES, null,fForTypes);
		return result;
	}

	public boolean removeForType(String item){
		boolean result = fForTypes.remove(item);
		propertyChangeSupport.fireIndexedPropertyChange(PN_FOR_TYPES, PI_FOR_TYPES, null,fForTypes);
		return result;
	}
	
	public void setForTypes(List<String> newForTypes) {
		if(newForTypes == null || newForTypes.equals(fForTypes)) return;
		List<String> oldForTypes = this.fForTypes;
		this.fForTypes = newForTypes;
		propertyChangeSupport.fireIndexedPropertyChange(PN_FOR_TYPES, PI_FOR_TYPES,
				oldForTypes,this.fForTypes);	
	}
}
